﻿Public Class Grid
    Inherits DataGridView

    Sub New()
        DoubleBuffered = True
        CellBorderStyle = DataGridViewCellBorderStyle.None
    End Sub

    Function Whiten(ByVal Col As Color, ByVal Percent As Integer) As Color
        Return Color.FromArgb((Col.R * (100 - Percent) + Percent * 255) / 100, (Col.G * (100 - Percent) + Percent * 255) / 100, (Col.B * (100 - Percent) + Percent * 255) / 100)
    End Function

    Function Blacken(ByVal Col As Color, ByVal Percent As Integer) As Color
        Return Color.FromArgb((Col.R * (100 - Percent)) / 100, (Col.G * (100 - Percent)) / 100, (Col.B * (100 - Percent)) / 100)
    End Function

    Sub AddColumn(ByVal Name As String, ByVal Label As String, ByVal Width As String, ByVal Alignment As String)
        Dim Column As New DataGridViewTextBoxColumn
        Column.ReadOnly = True
        Column.Name = Name
        Column.DataPropertyName = Name
        Column.HeaderText = Label
        Column.SortMode = DataGridViewColumnSortMode.NotSortable
        If Not AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None Then Width &= "%"
        If Not Width.Contains("%") Then
            Column.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet
            Column.Width = Width
        Else
            Column.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
            Column.FillWeight = Width.Replace("%", "")
        End If
        Select Case Alignment
            Case "c" : Column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : Column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            Case "r" : Column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight : Column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight
            Case "d" : Column.DefaultCellStyle.Format = "dd/MM/yyyy" : Column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter : Column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
        End Select
        Columns.Add(Column)
    End Sub

    Event Activate(ByVal sender As Object, ByVal e As EventArgs)

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Enter Then
            RaiseEvent Activate(Me, Nothing)
            Return True
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

    Protected Overrides Sub OnDoubleClick(ByVal e As System.EventArgs)
        MyBase.OnDoubleClick(e)
        RaiseEvent Activate(Me, Nothing)
    End Sub

End Class
